const service = require('./messageService.js');
const utils = require('../../../utils/util');
const app = getApp();
var page = 0
var systmPage = 0;
var chatPage = 0;
var noticePage = 0
Page({
  data: {
    globalData: {},
    tabs: [{ title: "公告消息", num: "" }, { title: "聊天消息", num: "" }, { title: "系统消息", num: "" } ],
    activeIndex: 0,
    systemMsg: [], //系统消息列表
    noticeMsg: [], //公告消息列表
    chatMsg: [],   //聊天消息列表
    noticeData: {},//公告消息接口所有信息
    systmData: {},  //系统消息接口所有信息
    chatData: {},   //聊天消息接口所有信息
    fromUserId: "",
    chatRoomId: null,
    scrollTop:0
  },
  onLoad: function () {
    systmPage = 0;
    chatPage = 0;
    noticePage = 0;
    this.setData({
      globalData: app.globalData
    })
    // service.requestChatMsg(page, (data) => {
    //   this.setData({chatMsg: data.items})
    // })
    // wx.showLoading({
    //   title: '加载中',
    // })
    
    
    // this.requestNoticeMsg()
    // this.requestUnReadChat()
    // this.requestUnReadSys()
  },
  //公告消息
  requestNoticeMsg:function(){
    var self = this;
    service.requestNoticeMsg(noticePage,0, (data) => {
      // wx.hideLoading()
      let arr = noticePage == 0 ? data.items : this.data.noticeMsg.concat(data.items)
      //已读
      self.data.tabs[0].num = 0
      if (data.unReadNotice > 0){
        service.readNotice(function(){
          // 刷新底部消息显示数量
          app.utilService.requestMessage();
        })
      }
      self.setData({ 
        noticeMsg: arr, 
        noticeData: data,
        tabs: self.data.tabs
       })
    });
  },
  //系统消息请求
  requestSystemMsg: function() {
    var self = this
    service.requestSystemMsg(systmPage, (data) => {
      // wx.hideLoading()
      let arr = systmPage == 0 ? data.items : self.data.systemMsg.concat(data.items)
      self.setData({systemMsg: arr, systmData: data})

      // 设置系统消息已读
      service.readMsg(
        function () {
          self.data.tabs[2].num = 0
          self.setData({
            tabs: self.data.tabs
          })
          // 刷新底部消息显示数量
          app.utilService.requestMessage();
        }
      )
    })
  },
  //聊天未读
  requestUnReadChat:function(){
    service.requestUnReadChat( (data) => {
      // wx.hideLoading()
      this.data.tabs[1].num = data.totalUnReadNum > 99 ? "99+" : data.totalUnReadNum
      this.setData({
        tabs:this.data.tabs
    })})
  },
  //系统未读
  requestUnReadSys:function(){
    service.requestUnReadSys((data) => {
      // wx.hideLoading()
      this.data.tabs[2].num = data.unReadMsgNum > 99 ? "99+" : data.unReadMsgNum
      this.setData({
        tabs: this.data.tabs
      })
    })
  },
  // 聊天消息请求
  requestChatMsg: function() {
    service.requestChatMsg(chatPage, (data) => {
      setTimeout(() => {  
        // wx.hideLoading();  
     }, 100);  
      let items = data.items.filter(item => {
        let timeStap = utils.timeToStamp(item.lastMessageTime);
        item.lastMessageTime = utils.getDateDiff(timeStap);
        return item.lastMessageContent !== "";
      });
      let arr = chatPage == 0 ? items : this.data.chatMsg.concat(items);
    
      this.setData({ 
        chatMsg: arr, 
        chatData: data,
        })

      // 刷新底部消息显示数量(聊天消息，请求列表成功，后台直接标记成已读。)
      app.utilService.requestMessage();
    })
  },

  // 点击进入聊天详情
  toDetails(e) {
    let fromUserId = e.currentTarget.dataset.fromuserid
    let chatRoomId = e.currentTarget.dataset.roomid
    let nickName
    let idx = e.currentTarget.dataset.idx
    let chatMsg = this.data.chatMsg
    let globalData = this.data.globalData
    nickName = chatMsg[idx].fromUserId == globalData.userId ? chatMsg[idx].toUserName : chatMsg[idx].fromUserName
    wx.navigateTo({
      url: '/pages/mine/message/msgDetails/msgDetails?fromUserId=' + fromUserId + '&chatRoomId=' + chatRoomId + '&nickName=' + nickName,
    })
  },
//调到公告
  toNotice(e){
    let idx = e.currentTarget.dataset.idx
    wx.navigateTo({
      url: '/pages/mine/message/noticeDetails/noticeDetails?noticeId=' + idx ,
    })
  },

  tabClick (e){
    this.setData({
      activeIndex: e.currentTarget.id,
      scrollTop: 0
    });
    
    if (this.data.activeIndex==2){//点击系统消息请求系统消息
      this.requestSystemMsg()
    } else if (this.data.activeIndex == 1){
      this.requestChatMsg()
      this.requestUnReadChat()
    } else if (this.data.activeIndex == 0){
      this.requestNoticeMsg()
    }
    console.log(e.currentTarget.id)
  },
  // 下拉刷新
  onPullDownRefresh: function(){
    // console.log("下拉刷新")
    wx.stopPullDownRefresh()
    if(this.data.activeIndex == 1){
      chatPage = 0
      this.requestChatMsg()
    } else if (this.data.activeIndex == 0) {
      noticePage=0;
        this.requestNoticeMsg()
    } else if (this.data.activeIndex == 2){
      systmPage = 0;
      this.requestSystemMsg()
    }
    this.requestUnReadChat()
    this.requestUnReadSys()
  },
  // 加载更多
  onReachBottom: function(){
    // console.log("加载更多")
    if(this.data.activeIndex == 1){
      if(this.data.chatData.hasNextPage == true){
        ++ chatPage 
        this.requestChatMsg()
      }
    } else if (this.data.activeIndex == 0) {
      if (this.data.chatData.hasNextPage == true) {
        ++ noticePage
        this.requestNoticeMsg()
      }
    }  else if (this.data.activeIndex == 2) {
      if(this.data.systmData.hasNextPage == true){
        ++ systmPage
        this.requestSystemMsg()
      }
    }
  },
  /**
    * 生命周期函数--监听页面显示
    */
  onShow: function () {
    this.requestNoticeMsg()
    this.requestUnReadChat()
    this.requestUnReadSys()
    this.requestChatMsg()
  },
  upper: function(e) {
  },
  lower: function(e) {
  },
  scroll: function(e) {
  },
});